package cn.hengzhu.main_manager.utils;

import cn.hengzhu.main_manager.repository.vo.CompBillVO;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class JSONToExcel {
	// public static void main(String[] args) {
	public static void excel(List<CompBillVO> array, HttpServletRequest request) {
		// 创建HSSFWorkbook对象
		HSSFWorkbook wb = new HSSFWorkbook();
		// 创建HSSFHeet对象
		HSSFSheet sheet = wb.createSheet("sheet0");
		// 创建HSSFRow对象
		// HSSFRow row = sheet.createRow(0);
		// 创建HSSFCell对象
		// HSSFCell cell = row.createCell(2);
		HSSFRow row1 = sheet.createRow(0);
		row1.createCell(0).setCellValue("付款人");
		row1.createCell(1).setCellValue("储物柜");
		row1.createCell(2).setCellValue("收款金额");
		row1.createCell(3).setCellValue("手续费");
		row1.createCell(4).setCellValue("收款时间");
		row1.createCell(5).setCellValue("公司名称");
		row1.createCell(6).setCellValue("平台");
		// 设置单元格的值
		for (int i = 0; i < array.size(); i++) {
			HSSFRow row = sheet.createRow(i + 1);
			// 创建HSSFRow对象
			// 创建HSSFCell对象
			row.createCell(0).setCellValue(array.get(i).getOpenId());
			row.createCell(1).setCellValue(array.get(i).getCaseNo());
			row.createCell(2).setCellValue(array.get(i).getPayAmount());
			row.createCell(3).setCellValue(array.get(i).getServiceCharge());
			row.createCell(4).setCellValue(array.get(i).getPayTime());
			row.createCell(5).setCellValue(array.get(i).getCompName());
			String source="";
			if(array.get(i).getSource()==2||array.get(i).getSource()==3){
				source = "微信";
			}else if(array.get(i).getSource()==4||array.get(i).getSource()==5){
				source = "支付宝";
			}
			row.createCell(6).setCellValue(source);
		}
		;


		String url = request.getSession().getServletContext().getRealPath("/") + "resources/账单.xls";

		FileOutputStream output = null;
		try {
			output = new FileOutputStream(url);
			wb.write(output);

			output.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != output) {
					output.close();
				}
				if (null != wb) {
					wb.close();

				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
